****************************************************************************
* This do file creates six-month mini panel from 1997 to 2012. 
****************************************************************************
*Brochu
* minimum wage project
* LFS

* program name: panel_1996_2012.do
* created: October 7, 2013
* updated: October 7, 2013
* last edited: March 5, 2019

clear


/* set directories */
* note:  no spaces allowed in directory names.

* rawdata is the directory that contains the raw data
local rawdata "V:\Townsend_5609\Iain_Feb27\Data"


* workdata is the directory that contains the working data sets
local workdata "V:\Townsend_5609\Iain_Feb27\WorkingData"





set more off

set memory 500m
set matsize 600

forvalues year1 = 1996/2016 { 

	forvalues month1 = 1/12 {
	
	// Load the LFS by year
	use `rawdata'\LFS`year1'.dta, replace

	**********************************************************
	**********************************************************
	* period 1 of the two month mini-panel
	**********************************************************
	**********************************************************
	
	**********************************************************
	* keeping current month data
	**********************************************************
	keep if smth==`month1'
	
	**********************************************************
	* keeping only those in the incoming rotation of period 1
	**********************************************************

	if `month1'<=6 {
		local incoming1 = `month1' 
		}
	else  { 
		local incoming1 = `month1' - 6 
		}
		
	keep if rotation == `incoming1' 


	**********************************************************

	rename syear syear1
	rename smth smth1
	rename tenure tenure1
	rename finalwt finalwt1
	rename cowmain cowmain1
	rename student student1
	rename educlev educlev1
	rename sex sex1
	rename relref relref1
	rename hrlyearn hrlyearn1
	rename prov prov1
	rename retstudt retstudt1
	rename birth birth1
	rename lfsstat lfsstat1
	rename age age1
	rename rcdo rcdo1
	rename tips tips1
	rename hrlywork hrlywork1
	rename membstat membstat1
	rename efamtype efamtype1
	rename mjh mjh1
	rename whyleft whyleft1
	rename WHYLEFT2 whyleftv2_1
	rename SIC5 sic1

	* Addressing the dec 1996 - jan 1997 seem for rotation 2
		
		replace line=line-10 if `year1'==1996 & `month1'>=8 & `month1'<=12 & rotation==2
		replace line=line-10 if `year1'==1997 & `month1'==1 & rotation==2
	

	**********************************************************
	* keeping only the merging variables (in capital letters)*
	* and other variables that are needed                    *
	**********************************************************

	
	keep PROV1 pseudoui frame strafram type clust rotation listline mult line   /*
	*/ syear1 smth1 tenure1 finalwt1 cowmain1 student1 educlev1 sex1 relref1     /*
	*/ hrlyearn1 prov1 retstudt1 birth1 lfsstat1 age1 rcdo1 tips1 hrlywork1 membstat1 efamtype1 mjh1 whyleft1 whyleftv2_1 sic1

	sort PROV1 pseudoui frame strafram type clust rotation listline mult line 

	save `workdata'\temp_data1.dta, replace

	

	**********************************************************
	**********************************************************
	
	
	
	**********************************************************
	**********************************************************
	* period 2 of the six month mini-panel
	**********************************************************
	**********************************************************
	
	
	if `month1'<12 {
		local month2 = `month1' +1
		local year2 = `year1'
	}
	else {
		local month2 = 1
		local year2 = `year1' +1

	}

	use `rawdata'\LFS`year2'.dta, replace
	
	
	**********************************************************
	* keeping current month data (as of period 1)
	**********************************************************
	keep if smth==`month2'

	**********************************************************
	* keeping only those in the incoming rotation of period 1
	**********************************************************

		keep if rotation==`incoming1' 
	**********************************************************
	

	rename syear syear2
	rename smth smth2
	rename tenure tenure2
	rename finalwt finalwt2
	rename cowmain cowmain2
	rename student student2
	rename educlev educlev2
	rename sex sex2
	rename relref relref2
	rename hrlyearn hrlyearn2
	rename prov prov2
	rename retstudt retstudt2
	rename birth birth2
	rename lfsstat lfsstat2
	rename age age2
	rename rcdo rcdo2
	rename tips tips2
	rename hrlywork hrlywork2
	rename membstat membstat2
	rename efamtype efamtype2
	rename mjh mjh2
	rename whyleft whyleft2
	rename WHYLEFT2 whyleftv2_2
	rename SIC5 sic2
	
	* Addressing the dec 1996 - jan 1997 seem for rotation 2
		
		replace line=line-10 if `year1'==1996 & `month1'>=8 & `month1'<=12 & rotation==2
		replace line=line-10 if `year1'==1997 & `month1'==1 & rotation==2
		
	**********************************************************
	* keeping only the merging variables (in capital letters)*
	* and other variables that are needed                    *
	**********************************************************

	
	keep PROV1 pseudoui frame strafram type clust rotation listline mult line   /*
	*/ syear2 smth2 tenure2 finalwt2 cowmain2 student2 educlev2 sex2 relref2   /*
	*/ hrlyearn2 prov2 retstudt2 birth2 lfsstat2 age2 rcdo2 tips2 hrlywork2 membstat2 efamtype2 mjh2 whyleft2 whyleftv2_2 sic2
	
	sort PROV1 pseudoui frame strafram type clust rotation listline mult line 

	save `workdata'\temp_data2.dta, replace

	clear

	**********************************************************
	**********************************************************


	**********************************************************
	**********************************************************
	* period 3 of the six month mini-panel
	**********************************************************
	**********************************************************
	
	if `month1'<11 {
		local month3 = `month1' + 2
		local year3 = `year1'
	}
	else {
		local month3 = `month1' + 2 - 12
		local year3 = `year1' + 1
	}

	use `rawdata'\LFS`year3'.dta, replace
		
	**********************************************************
	* keeping current month data (as of period 1)
	**********************************************************
	keep if smth == `month3'

	**********************************************************
	* keeping only those in the incoming rotation of period 1
	**********************************************************

	keep if rotation==`incoming1' 
	**********************************************************
	
	rename syear syear3
	rename smth smth3
	rename tenure tenure3
	rename finalwt finalwt3
	rename cowmain cowmain3
	rename student student3
	rename educlev educlev3
	rename sex sex3
	rename relref relref3
	rename hrlyearn hrlyearn3
	rename prov prov3
	rename retstudt retstudt3
	rename birth birth3
	rename lfsstat lfsstat3
	rename age age3
	rename rcdo rcdo3
	rename tips tips3
	rename hrlywork hrlywork3
	rename membstat membstat3
	rename efamtype efamtype3
	rename mjh mjh3
	rename whyleft whyleft3
	rename WHYLEFT2 whyleftv2_3
	rename SIC5 sic3
	
	* Addressing the dec 1996 - jan 1997 seem for rotation 2
	replace line=line-10 if `year1'==1996 & `month1'>=8 & `month1'<=12 & rotation==2
	replace line=line-10 if `year1'==1997 & `month1'==1 & rotation==2
		
	**********************************************************
	* keeping only the merging variables (in capital letters)*
	* and other variables that are needed                    *
	**********************************************************

	keep PROV1 pseudoui frame strafram type clust rotation listline mult line   /*
	*/ syear3 smth3 tenure3 finalwt3 cowmain3 student3 educlev3 sex3 relref3    /*
	*/ hrlyearn3 prov3 retstudt3 birth3 lfsstat3 age3 rcdo3 tips3 hrlywork3 membstat3 efamtype3 mjh3 whyleft3 whyleftv2_3 sic3
	
	sort PROV1 pseudoui frame strafram type clust rotation listline mult line 

	save `workdata'\temp_data3.dta, replace

	clear

	**********************************************************
	**********************************************************

	**********************************************************
	**********************************************************
	* period 4 of the six month mini-panel
	**********************************************************
	**********************************************************
	
	if `month1' < 10 {	// ### Modify
		local month4 = `month1' + 3
		local year4 = `year1'
	}
	else {
		local month4 = `month1' + 3 - 12
		local year4 = `year1' + 1
	}

	
	
	use `rawdata'\LFS`year4'.dta, replace
		
	**********************************************************
	* keeping current month data (as of period 1)
	**********************************************************
	keep if smth == `month4' // ### Modify

	**********************************************************
	* keeping only those in the incoming rotation of period 1
	**********************************************************

	keep if rotation==`incoming1' 
	**********************************************************
	
	rename syear syear4
	rename smth smth4
	rename tenure tenure4
	rename finalwt finalwt4
	rename cowmain cowmain4
	rename student student4
	rename educlev educlev4
	rename sex sex4
	rename relref relref4
	rename hrlyearn hrlyearn4
	rename prov prov4
	rename retstudt retstudt4
	rename birth birth4
	rename lfsstat lfsstat4
	rename age age4
	rename rcdo rcdo4
	rename tips tips4
	rename hrlywork hrlywork4
	rename membstat membstat4
	rename efamtype efamtype4
	rename mjh mjh4
	rename whyleft whyleft4
	rename WHYLEFT2 whyleftv2_4
	rename SIC5 sic4
	
	* Addressing the dec 1996 - jan 1997 seem for rotation 2
	replace line=line-10 if `year1'==1996 & `month1'>=8 & `month1'<=12 & rotation==2
	replace line=line-10 if `year1'==1997 & `month1'==1 & rotation==2
		
	**********************************************************
	* keeping only the merging variables (in capital letters)*
	* and other variables that are needed                    *
	**********************************************************
//### Modify
	keep PROV1 pseudoui frame strafram type clust rotation listline mult line   /*
	*/ syear4 smth4 tenure4 finalwt4 cowmain4 student4 educlev4 sex4 relref4   /*
	*/ hrlyearn4 prov4 retstudt4 birth4 lfsstat4 age4 rcdo4 tips4 hrlywork4 membstat4 efamtype4 mjh4 whyleft4 whyleftv2_4 sic4
	
	sort PROV1 pseudoui frame strafram type clust rotation listline mult line 
// ### Modify
	save `workdata'\temp_data4.dta, replace

	clear

	**********************************************************
	**********************************************************	
	
	**********************************************************
	**********************************************************
	* Merging the six periods, and saving the mini-panel     *
	**********************************************************
	**********************************************************
	
	use `workdata'\temp_data1.dta
	merge 1:1 (PROV1 pseudoui frame strafram type clust rotation listline mult line) using `workdata'\temp_data2.dta

	sum syear1 smth1 smth2
	sum rotation if _merge==1
	sum rotation if _merge==2
	drop _merge

	// Merge the panel for month 3
	merge 1:1 (PROV1 pseudoui frame strafram type clust rotation listline mult line) using `workdata'\temp_data3.dta

	sum syear1 smth1 smth2 smth3
	sum rotation if _merge==1
	sum rotation if _merge==2
	drop _merge
	
// ### Modify
	// Merge the panel for month 4
	merge 1:1 (PROV1 pseudoui frame strafram type clust rotation listline mult line) using `workdata'\temp_data4.dta
	
	sum syear1 smth1 smth2 smth3 smth4
	sum rotation if _merge==1
	sum rotation if _merge==2
	drop _merge
	
	save `workdata'\panel_6M_`month1'_`year1'.dta, replace
	
	}
	

}

